<?php

namespace app\api\controller\user;

use think\Controller;
use think\Request;

class Base extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel = model('User.User');
    }

    //注册
    public function register()
    {
        //判断是否为post请求
        if ($this->request->isPost()) {
            $params = $this->request->param();

            $password = $this->request->param('password', '');

            if (empty($password)) {
                $this->error('密码不能为空');
                exit;
            }

            //生成密码盐
            $salt = build_ranstr(10);

            //将密码和密码盐md5加密
            $password = md5(trim($password) . $salt);

            //获取系统配置表里面的赠送积分
            $RegisterPoint = model('Config')->where(['key' => 'RegisterPoint'])->value('value');

            //判断一下注册积分 是不是一个整数
            $RegisterPoint - is_numeric($RegisterPoint) ? abs($RegisterPoint) : 0;

            //组装数据
            $data = [
                'email' => $params['email'],
                'password' => $password,
                'salt' => $salt,
                'nickname' => $params['email'],
                'point' => $RegisterPoint,
                'vip' => 0,
                'auth' => 0
            ];

            //开始插入数据库
            $result = $this->UserModel->validate('common/user/User')->save($data);

            if ($result === FALSE) {
                $this->error($this->UserModel->getError());
                exit;
            } else {
                $this->success('注册成功');
                exit;
            }
        }
    }

    //登录
    public function login()
    {
        //判断是否有post提交
        if ($this->request->isPost()) {

            $email = $this->request->param('email', '');
            $password = $this->request->param('password', '');

            //根据邮箱查询该条数据
            $User = $this->UserModel->where(['email' => $email])->find();

            //没有找到用户的情况下
            if ($User === FALSE) {
                $this->error('用户不存在,请重新输入');
                exit;
            }

            //判断密码
            $salt = $User['salt'];
            $password = md5(trim($password) . $salt);

            if ($password != $User['password']) {
                $this->error('密码错误,请重新输入');
                exit;
            }

            //设置一个保存cookie信息
            $data = [
                'id' => $User['id'],
                'email' => $User['email'],
                'nickname' => $User['nickname'],
                'avatar' => $User['avatar_cdn'],
                'sex' => $User['sex'],
                'province' => $User['province'],
                'city' => $User['city'],
                'district' => $User['district'],
                'createtime' => $User['createtime'],
                'content' => $User['content'],
                'point' => $User['point'],
                'vip' => $User['vip'],
                'auth' => $User['auth'],
                'cover' => $User['cover'],
                'region' => $User['region'],

            ];

            //跳转界面
            $this->success('登录成功', '/user/base/index', $data);
            exit;
        }
    }

    //验证是否登录
    public function check()
    {
        //判断是否是post请求
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);

            // 查询这个用户是否存在
            $User = $this->UserModel->find($userid);

            if ($User) {
                //设置一个保存cookie信息
                $data = [
                    'id' => $User['id'],
                    'email' => $User['email'],
                    'nickname' => $User['nickname'],
                    'avatar' => $User['avatar_cdn'],
                    'sex' => $User['sex'],
                    'province' => $User['province'],
                    'city' => $User['city'],
                    'district' => $User['district'],
                    'createtime' => $User['createtime'],
                    'content' => $User['content'],
                    'point' => $User['point'],
                    'vip' => $User['vip'],
                    'auth' => $User['auth'],
                    'cover' => $User['cover_cdn'],
                    'region' => $User['region'],
                ];

                $this->success('登录成功', null, $data);
                exit;
            } else {
                $this->error('用户不存在');
                exit;
            }
        }
    }

    //邮箱验证
    public function email()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);

            //判断用户是否存在
            $user = $this->UserModel->find($userid);

            if (!$user) {
                $this->error('用户不存在');
                exit;
            }

            //判断是否认证过
            if ($user['auth'] == 1) {
                $this->error('您已通过认证,无需重复认证');
                exit;
            }

            if (empty($user['email'])) {
                $this->error('邮箱不能为空');
                exit;
            }

            //发送邮箱验证
            //将邮箱md5加密
            $email = md5($user['email']);

            //组装信息
            $msg = "<a href='http://www.asks.com/home/index/email?email=$email'>点击免费邮箱验证</a>";

            //调用公共函数库里面邮箱验证发送方法
            $result = send($user['email'], $msg);


            //返回结果
            $success = [
                'result' => false,
                'msg' => ''
            ];

            if ($result) {
                $success['result'] = true;
                $success['msg'] = '发送邮件成功,请注意查收';
            } else {
                $success['result'] = false;
                $success['msg'] = '邮件发送失败,请重新发送';
            }

            return json($success);
        }
    }

    //更改个人信息
    public function profile()
    {
        if ($this->request->isPost()) {

            //获取所有参数
            $params = $this->request->param();

            $userid = $this->request->param('id', 0);
            $password = $this->request->param('password', '');

            //先判断这个人是否存在
            $user = $this->UserModel->find($userid);

            if (!$user) {
                $this->error('用户不存在');
            }

            //组装数据
            $data = [
                'id' => $userid,
                'nickname' => $params['nickname'],
                'email' => $params['email'],
                'sex' => $params['sex'],
                'content' => $params['content'],
                'province' => $params['province'],
                'city' => $params['city'],
                'district' => $params['district'],
            ];

            //判断是否修改了邮箱
            if ($user['email'] != $params['email']) {
                $data['auth'] = 0;
            }

            //密码不为空
            if (!empty($password)) {
                $salt = $user['salt'];

                $repass = md5($password . $salt);

                //判断新密码旧密码是否一致

                if ($repass == $user['password']) {
                    $this->error('当前密码为旧密码,请重新输入');
                    exit;
                }

                //生成新的密码盐
                $newsalt = build_ranstr(10);

                //新密码加密
                $password = md5(trim($password) . $newsalt);

                $data['salt'] = $newsalt;
                $data['password'] = $password;
            }

            //判断头像
            if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
                $result = upload('avatar');

                if ($result === FALSE) {
                    $this->error('修改头像失败');
                    exit;
                }

                //追加头像
                $data['avatar'] = $result;
            }

            //封面
            if (isset($_FILES['cover']) && $_FILES['cover']['size'] > 0) {
                $result = upload('cover');

                if ($result === FALSE) {
                    $this->error('修改封面失败');
                    exit;
                }

                //追加头像
                $data['cover'] = $result;
            }

            //更新数据库
            $result = $this->UserModel->isUpdate(true)->save($data);

            if ($result === FALSE) {
                //把上传上来的图片删了
                @is_file($data['avatar']) && @unlink($data['avatar']);
                @is_file($data['cover']) && @unlink($data['cover']);

                $this->error('修改个人资料失败');
                exit;
            } else {
                //先判断是否有选择新图片(判断新插入的图片路径是否有效)
                if (@is_file($data['avatar'])) {
                    //修改成功后，将原来的图片删除
                    $avatar = ltrim($user['avatar'], "/");
                    @is_file($avatar) && @unlink($avatar);
                }

                //先判断是否有选择新图片(判断新插入的图片路径是否有效)
                if (@is_file($data['cover'])) {
                    //修改成功后，将原来的图片删除
                    $cover = ltrim($user['cover'], "/");
                    @is_file($cover) && @unlink($cover);
                }

                //直接查询最新的结果在返回
                $UserData = $this->UserModel->find($userid);

                $data = [
                    'id' => $UserData['id'],
                    'email' => $UserData['email'],
                    'nickname' => $UserData['nickname'],
                    'avatar' => $UserData['avatar_cdn'],
                    'sex' => $UserData['sex'],
                    'province' => $UserData['province'],
                    'city' => $UserData['city'],
                    'district' => $UserData['district'],
                    'createtime' => $UserData['createtime'],
                    'content' => $UserData['content'],
                    'point' => $UserData['point'],
                    'vip' => $UserData['vip'],
                    'auth' => $UserData['auth'],
                    'cover' => $UserData['cover_cdn'],
                    'region' => $UserData['region'],
                ];

                $this->success('修改资料成功', '/user/base/index', $data);
                exit;
            }
        }
    }
}
